package com.ws.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ws.eduservice.entity.EduSubject;
import com.ws.eduservice.entity.excel.ExcelSubject;
import com.ws.eduservice.entity.subject.OneSubject;
import com.ws.eduservice.entity.subject.TwoSubject;
import com.ws.eduservice.listener.SubjectExcelListener;
import com.ws.eduservice.mapper.EduSubjectMapper;
import com.ws.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ws.servicebase.exceptionhandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author ws
 * @since 2024-12-04
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void readFileSave(EduSubjectService subjectService, MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, ExcelSubject.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            throw new MyException(20001,"添加课程失败");
        }


    }

    @Override
    public List<OneSubject> getOneTwoSubject() {
        //获取所有的一级分类
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",0);
        List<EduSubject> oneEduSubjects = baseMapper.selectList(queryWrapper);
        //获取所有的二级分类
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.ne("parent_id",0);
        List<EduSubject> twoEduSubjects = baseMapper.selectList(queryWrapper2);

        //封装一级分类
        List<OneSubject> oneSubjects=new ArrayList<>();
        for (EduSubject oneEduSubject : oneEduSubjects) {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(oneEduSubject,oneSubject);
            oneSubjects.add(oneSubject);
            //封装二级分类
            List<TwoSubject> twoSubjects=new ArrayList<>();
            for (EduSubject twoEduSubject : twoEduSubjects) {
                if (oneEduSubject.getId().equals(twoEduSubject.getParentId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(twoEduSubject,twoSubject);
                    twoSubjects.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoSubjects);

        }


//        TwoSubject twoSubject = null;
//        List<TwoSubject> twoSubjects=null;
//        for (OneSubject one : oneSubjects) {
//            twoSubjects = new ArrayList();
//            for (EduSubject twoEduSubject : twoEduSubjects) {
//                twoSubject = new TwoSubject();
//                if (one.getId().equals(twoEduSubject.getParentId())) {
//                    BeanUtils.copyProperties(twoEduSubject,twoSubject);
//                    twoSubjects.add(twoSubject);
//                }
//            }
//            one.setChildren(twoSubjects);
//        }

//        for (EduSubject oneEduSubject : oneEduSubjects) {
//            oneSubject = new OneSubject();
//            twoSubjects=new ArrayList<>();
//            for (EduSubject twoEduSubject : twoEduSubjects) {
//                twoSubject=new TwoSubject();
//                if (oneEduSubject.getId().equals(twoEduSubject.getParentId())) {
//                    twoSubject.setId(twoEduSubject.getId());
//                    twoSubject.setTitle(twoEduSubject.getTitle());
//                    twoSubjects.add(twoSubject);
//                    oneSubject.setId(oneEduSubject.getId());
//                    oneSubject.setTitle(oneEduSubject.getTitle());
//                }
//                oneSubject.setChildren(twoSubjects);
//            }
//            oneSubjects.add(oneSubject);
//        }
//
//        System.out.println(oneSubjects);
        return oneSubjects;


    }
}
